#include <my_header.h>

int ls_al(const char* dir);

/* Usage:  */
int main(int argc, char *argv[]){                                  
    ARGS_CHECK(argc, 2);
    int ret = ls_al(argv[1]);

    return ret;
}

int ls_al(const char* dir){
    // Check dir.
    DIR *dirp = opendir(dir);
    ERROR_CHECK(dirp, NULL, "opendir");
    
    // Change dir.
    int ret = chdir(dir);
    ERROR_CHECK(ret, -1, "chdir");
    
    // Save the dir info.
    struct dirent *pdirent;
    
    while(1){
        pdirent = readdir(dirp);
        if(pdirent == NULL){
            break;
        }

        // get the dir info.
        struct stat stat_buf;
        int ret = stat(pdirent->d_name, &stat_buf);
        ERROR_CHECK(ret, -1, "stat");

        // print the info
        printf("%o, %lu, %u, %u, %lu, %lu, %s\n",
               stat_buf.st_mode,    // print mod
               stat_buf.st_nlink,
               stat_buf.st_uid,
               stat_buf.st_gid,
               stat_buf.st_size,
               stat_buf.st_mtim.tv_sec,
               pdirent->d_name
               );
    }
        // close the dir
        closedir(dirp);
        return 0;
}
